package com.dingreading.cloud.soms.service.impl;

import com.dingreading.cloud.common.util.NepUtil;
import com.dingreading.cloud.soms.dto.StoreProjectCycleDtoV2;
import com.dingreading.cloud.soms.dto.StoreProjectCycleDtoV3;
import com.dingreading.cloud.soms.entity.StoreProjectCycle;
import com.dingreading.cloud.soms.entity.table.StoreProjectCycleTableDef;
import com.dingreading.cloud.soms.mapper.StoreProjectCycleMapper;
import com.dingreading.cloud.soms.service.StoreProjectCycleService;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2024-09-11
 */
@Service
public class StoreProjectCycleServiceImpl extends BaseServiceImpl<StoreProjectCycleMapper, StoreProjectCycle, StoreProjectCycleTableDef> implements StoreProjectCycleService {

    @Override
    protected StoreProjectCycleTableDef getTable() {
        return StoreProjectCycleTableDef.storeProjectCycle;
    }

    @Resource
    private StoreProjectCycleMapper cycleMapper;


    @Override
    public boolean haveNameV2(String storeUid, String uid, String projectUid, String name) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.projectUid.eq(projectUid))
                .and(table.cycleName.eq(name))
                .and(
                        table.storeUid.isNull().and(table.source.eq(1))
                                .or(table.storeUid.eq(storeUid)).and(table.source.eq(2))
                );
        if (StringUtils.isNotBlank(uid))
            condition.and(table.uid.ne(uid));

        return exists(condition);
    }

    @Override
    public boolean updateEnabledV2(String storeUid, String uid, Integer enabled, String staffUid, String staffName) {
        return UpdateChain.of(StoreProjectCycle.class)
                .set(table.enabled, enabled)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName))
                .where(table.storeUid.eq(storeUid).and(table.uid.eq(uid)))
                .update();
    }

    @Override
    public int dropBeanV2(String storeUid, String uid) {
        return getMapper().deleteByCondition(table.storeUid.eq(storeUid).and(table.uid.eq(uid)));
    }

    @Override
    public StoreProjectCycle getGtOrderNumV2(String storeUid, String projectUid, Integer orderNum) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.eq(storeUid))
                .and(table.orderNum.gt(orderNum));
        if (StringUtils.isNotBlank(projectUid))
            condition.and(table.projectUid.eq(projectUid));
        else
            condition.and(table.projectUid.isNull());

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.asc());

        return getMapper().selectOneByQuery(wrapper);
    }

    @Override
    public StoreProjectCycle getLtOrderNumV2(String storeUid, String projectUid, Integer orderNum) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.storeUid.eq(storeUid))
                .and(table.orderNum.lt(orderNum));
        if (StringUtils.isNotBlank(projectUid))
            condition.and(table.projectUid.eq(projectUid));
        else
            condition.and(table.projectUid.isNull());

        QueryWrapper wrapper = QueryWrapper.create()
                .where(condition)
                .orderBy(table.orderNum.desc());

        return getMapper().selectOneByQuery(wrapper);
    }

    @Override
    public boolean updateOrderNum(String uid, Integer orderNum, String staffUid, String staffName) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.uid.eq(uid));
        return UpdateChain.of(StoreProjectCycle.class)
                .set(table.orderNum, orderNum)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName))
                .where(condition)
                .update();
    }

    @Override
    public StoreProjectCycle getByUid(String uid) {
        return getMapper().selectOneByCondition(table.uid.eq(uid));
    }

    @Override
    public boolean updateInfo(
            String storeUid, String uid, String cycleName, Integer days, Integer lessons, Double totalAmount, Double unitAmount,
            String staffUid, String staffName
    ) {
        QueryCondition condition = table.uid.eq(uid)
                .and(table.storeUid.eq(storeUid));
        return UpdateChain.of(StoreProjectCycle.class)
                .set(table.cycleName, cycleName)
                .set(table.days, days)
                .set(table.lessons, lessons)
                .set(table.totalAmount, totalAmount)
                .set(table.unitAmount, unitAmount)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName))
                .where(condition)
                .update();
    }

    @Override
    public StoreProjectCycle getByCycleConfig(boolean selfStore, String storeUid, String uid) {
        return cycleMapper.configByCycleUid(selfStore, storeUid, uid);
    }

    @Override
    public List<StoreProjectCycleDtoV2> listByCycleConfig(boolean selfStore, String storeUid, String projectUid) {
        return cycleMapper.configListByStoreUidAndProjectUid(selfStore, storeUid, projectUid);
    }

    @Override
    public List<StoreProjectCycleDtoV3> configListByStoreUidAndProjectUidIn(boolean selfStore, String storeUid, List<String> projectUidList) {
        return cycleMapper.configListByStoreUidAndProjectUidInAndEnabled(selfStore, storeUid, projectUidList);
    }

    @Override
    public int getMaxShowOrderV2(String storeUid, String projectUid) {
        QueryCondition condition = QueryCondition.createEmpty();
        if (StringUtils.isNotBlank(storeUid))
            condition.and(table.storeUid.eq(storeUid).or(table.storeUid.isNull()));
        else
            condition.and(table.storeUid.isNull());
        condition.and(table.projectUid.eq(projectUid));

        QueryWrapper wrapper = QueryWrapper.create()
                .select(QueryMethods.max(table.orderNum))
                .from(table)
                .where(condition);

        Integer integer = getMapper().selectObjectByQueryAs(wrapper, Integer.class);
        return NepUtil.nullToZero(integer);
    }

    @Override
    public boolean setEnabled(
            String storeUid, String uid, String cycleName, Integer days, Integer lessons,
            Double totalAmount, Double unitAmount, String staffUid, String staffName
    ) {
        return UpdateChain.of(StoreProjectCycle.class)
                .set(table.cycleName, cycleName)
                .set(table.days, days)
                .set(table.lessons, lessons)
                .set(table.totalAmount, totalAmount)
                .set(table.unitAmount, unitAmount)
                .set(table.enabled, 1)
                .set(table.lastStaffUid, NepUtil.nullToZero(staffUid))
                .set(table.lastStaffName, NepUtil.nullToZero(staffName))
                .where(table.storeUid.eq(storeUid).and(table.uid.eq(uid)))
                .update();
    }

    @Override
    public void dropByProjectUid(String storeUid, String projectUid) {
        QueryCondition condition = table.storeUid.eq(storeUid)
                .and(table.projectUid.eq(projectUid));
        getMapper().deleteByCondition(condition);
    }

}
